#ifndef FILTERS_H_
#define FILTERS_H_



#endif /* FILTERS_H_ */

#include <math.h>

#include <inttypes.h>
#include <stdbool.h>
#include <string.h>
//POT1 cut-off, POT2 Q-value
//If q-value = 0, filter off
float** biquad_gen(char * type, float Fc, float Q, float Fs){
	float** val = (float**) malloc(sizeof(float)*3*2);
	float a[4] = {0};
	float b[4] = {0};
	float w0 = 2 * M_PI * Fc / Fs;
	float c1 = cos(w0);
	float alpha = sin(w0) / (2 * Q);

	int i,j;
	if (!strcmp(type, 'LPF')){
		    b0 = (1 - c1)/2;
            b1 = 1 - c1;
            b2 = (1-c1)/2;
            a0 = 1 + alpha;
            a1 = -2 * c1;
            a2 = 1 - alpha;
	} else if (!strcmp(type, 'HPF')) {
			b0 = (1 + c1)/2;
            b1 = -1 - c1;
            b2 = (1 + c1)/2;
            a0 = 1 + alpha;
            a1 = -2 * c1;
            a2 = 1 - alpha;
	} else if (!strcmp(type, 'BPF')) {
			b0 = alpha;
            b1 = 0;
            b2 = -alpha;
            a0 = 1 + alpha;
            a1 = -2 * c1;
            a2 = 1 - alpha;
	} else if (!strcmp(type, 'NPF')) {
			b0 = 1;
            b1 = -2*c1;
            b2 = 1;
            a0 = 1 + alpha;
            a1 = -2*c1;
            a2 = 1 - alpha;
	}
	b[0] = b0/a0;
    b[1] = b1/a0;
    b[2] = b2/a0;
    
    a[0] = 1; // a(1) is always 1 in FIR/IIR
    a[1] = a1/a0;
    a[2] = a2/a0;
	
	for (i = 0; i < 3; j++){
		val[0][i] = a[i];
	}

	for (i = 0; i < 3; j++){
		val[1][i] = b[i];
	}
	return **val;
}

float delay_fx(float inp[], float alpha, int delay){
	int i = 0;
	len = sizeof(inp)/sizeof(float);
	float out[len] = {0};
	
	for (i = 0; i < len - delay; i++){
		if (i < delay + 1){
			out[i] = inp[i];
		} else {
			out[i] = inp[i] + inp[i - delay] * alpha;
		}
	}
	return *out;
}

float delay_fx(float inp[], float alpha, int delay){

}


float echo_fx(float inp[], float alpha, int delay){
	int i = 0;
	len = sizeof(inp)/sizeof(float);
	float out[len] = {0};
	
	for (i = 0; i < len - delay; i++){
		if (i < delay + 1){
			out[i] = inp[i];
		} else {
			out[i] = inp[i] + out[i - delay] * alpha;
		}
	}
	return *out;
}


float crusher_fx(float i, float bits)
{
int bits=16;
float rate=0.5;

long int m=1<<(bits-1);
float y=0,cnt=0;

cnt+=rate;
if (cnt>=1)
{
  cnt-=1;
  y=(long int)(i*m)/(float)m;
}
return y;
}
